<template>
  <div class="app-container">
    <uc-table url="/admin/enterpriseTask/getEnterpriseTaskPage" name="任务发布" ref="taskReleaseTable" pageable table-border
              :search-form="searchForm">
      <template slot="searchForm">
        <el-form size="mini" :inline="true" :model="searchForm" class="demo-form-inline filter-container">
          <!--搜索输入框-->
          <el-form-item>
            <el-input type="text" v-model="searchForm.name" placeholder="请输入任务名称" clearable
                      @clear="clearInput('name')"></el-input>
          </el-form-item>
          <el-form-item>
            <el-select v-model="searchForm.status" placeholder="请选择领取状态" clearable @clear="clearInput('status')">
              <el-option value="0" label="未领取"></el-option>
              <el-option value="1" label="已领取"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="searchForm.type" placeholder="请选择任务类型" clearable @clear="clearInput('type')">
              <el-option
                  v-for="item in taskTypeList"
                  :key="item.id"
                  :label="item.taskName"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="enterPriseHidden">
            <el-select v-model="searchForm.enterprise" placeholder="请选择所属主体" clearable>
              <el-option v-for="(item,index) in enterpriseDominantList" :key="index" :value="item.ucId"
                         :label="item.name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="!enterPriseHidden">
            <el-select v-model="searchForm.region" placeholder="请选择所属区域" clearable @clear="clearInput('region')">
              <el-option v-for="(item,index) in regionList" :key="index" :value="item.ucId"
                         :label="item.name"></el-option>
            </el-select>
          </el-form-item>
          <!--搜索按钮--新增按钮-->
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" class="filter-item" @click="handleSearch()">查询</el-button>
            <el-button type="success" icon="el-icon-plus" class="filter-item" @click="handleAdd()">新 增</el-button>
            <el-button type="success" icon="el-icon-setting" class="filter-item" @click="handleBuild()">巡检任务统一配置
            </el-button>
          </el-form-item>
        </el-form>
      </template>
      <template slot="columns">
        <el-table-column prop="enterpriseName" label="主体名称" align="left" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="name" label="任务名称" align="left" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="type" label="任务类型" align="left">
          <template slot-scope="scope">
            {{ scope.row.type| taskType }}
          </template>
        </el-table-column>
        <el-table-column prop="regionName" label="任务区域" align="left" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="remark" label="任务要求" align="left" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="content" label="任务内容" align="left" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="status" label="领取状态" align="left" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            {{ scope.row.status | receiveStatus }}
          </template>
        </el-table-column>
        <el-table-column prop="executorName" label="领取人" align="left" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="操作" width="250" align="left" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <!--编辑按钮-->
            <el-button size="mini" icon="el-icon-edit" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" icon="el-icon-info" type="success" @click="handleInfo(scope.row)">详情</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" :loading="deleteLoading"
                       @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </template>
    </uc-table>

    <!-- 生成及重置巡检任务配置信息对话框 -->
    <el-dialog title="任务配置" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="任务类型" prop="taskName">
              <el-input v-model="typeName" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="任务要求" prop="remark">
              <el-input type="textarea" :autosize="{ minRows: 2}" v-model="form.remark" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务开始时间" prop="startTime">
              <el-time-picker
                  v-model="form.startTime"
                  value-format="yyyy-MM-dd HH:mm"
                  :picker-options="{selectableRange: '00:00:00 - 23:59:59',format:'HH:mm'}"
              >
              </el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务结束时间" prop="endTime">
              <el-time-picker
                  v-model="form.endTime"
                  value-format="yyyy-MM-dd HH:mm"
                  :picker-options="{selectableRange: '00:00:00 - 23:59:59',minTime:form.startTime,format:'HH:mm'}"
              >
              </el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="任务内容" prop="content">
              <el-input type="textarea" :autosize="{ minRows: 4}" v-model="form.content" placeholder="请输入任务内容"/>
            </el-form-item>
          </el-col>
          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="提醒时间" prop="callValue">-->
          <!--              <el-time-picker-->
          <!--                  v-model="form.callTime"-->
          <!--                  value-format="yyyy-MM-dd HH:mm:ss"-->
          <!--                  :picker-options="{selectableRange: '00:00:00 - 23:59:59'}"-->
          <!--              ></el-time-picker>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="提醒岗位" prop="callPost">-->
          <!--              <el-select v-model="callPostList" multiple filterable placeholder="请选择">-->
          <!--                <el-option-->
          <!--                    v-for="item in postList"-->
          <!--                    :key="item.postCode"-->
          <!--                    :label="item.postName"-->
          <!--                    :value="item.postCode">-->
          <!--                </el-option>-->
          <!--              </el-select>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
        </el-row>
      </el-form>
      <div slot="footer" align="center">
        <el-button type="primary" @click="submitForm">生成巡检任务</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import UcTable from "@/components/UcTable/index.vue";
import {deleteInTaskRelease, buildPatrolTask, getPatrolTask} from "@/api/enterpriseTask";
import {listTaskType} from "@/api/taskType";
import {getInEnterpriseDominantList} from "@/api/enterpriseDominant";
import {getRegionList} from "@/api/enterprise";

export default {
  name: "UserTaskList",
  components: {UcTable},
  data() {
    return {
      searchForm: {
        status: null,
        type: null,
        deleted: false,
      },
      deleteLoading: false,
      enterpriseDominantList: [],
      regionList: [],
      //任务类型
      taskTypeList: [],
      // 是否显示弹出层
      open: false,
      // 岗位选择项
      postList: [],
      typeName: "巡检任务",
      callPostList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        startTime: [{required: true, message: "任务开始时间不能为空"}],
        endTime: [{required: true, message: "任务结束时间不能为空"}],
      }
    }
  },
  computed: {
    enterPriseHidden() {
      let roleCode = this.$store.getters.roleCode;
      return roleCode == "ADMIN" || roleCode == "XT-ADMIN" || roleCode == "ZD-ADMIN" || roleCode == "DD-ADMIN" || roleCode == "JW-ADMIN"
    }
  },
  created() {
    this.getRegionList()
    this.getTaskTypeList()
    this.getInEnterpriseDominantList()
  },
  methods: {
    //获取主体
    getInEnterpriseDominantList() {
      getInEnterpriseDominantList({deleted: false,dominantType:6}).then(res => {
        this.enterpriseDominantList = res.data
      })
    },
    //获取巡检任务的名称及要求
    getTaskTypeList() {
      listTaskType({}).then(res => {
        this.taskTypeList = res.data
      })
    },
    handleSearch() {
      this.$refs.taskReleaseTable.getTableData(1)
    },
    handleAdd() {
      this.$router.push("/taskManage/task/edit/0")
    },
    handleEdit(val) {
      this.$router.push("/taskManage/task/edit/" + val.ucId)
    },
    handleInfo(val) {
      this.$router.push("/taskManage/task/info/" + val.ucId)
    },
    clearInput(val) {
      //清除输入框
      this.searchForm[val] = null
      this.handleSearch()
    },
    handleDelete(val) {
      this.$confirm('您确定要执行删除' + val.name + '这个任务吗？', '提醒信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteLoading = true
        deleteInTaskRelease(val).then(res => {
          this.deleteLoading = false
          this.$notify.success({
            title: '成功',
            message: "删除任务成功"
          })
          this.handleSearch()
        }).catch(res => {
          this.deleteLoading = false
          this.$notify.error({
            title: '失败',
            message: res.msg
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    getRegionList() {
      getRegionList({}).then(res => {
        this.regionList = res.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.callPostList = []
      this.form = {
        id: undefined,
        remark: undefined,
        startTime: undefined,
        endTime: undefined,
        callTime: undefined,
        callValue: undefined,
        callPost: undefined,
        content: undefined
      };
      this.resetForm("form");
    },
    /** 任务配置按钮 */
    handleBuild() {
      this.reset();
      //获取巡检任务配置信息
      getPatrolTask().then(res => {
        if (res.data) {
          this.form.enterprise = res.data.enterprise
          this.form.startTime = res.data.startTime
          this.form.endTime = res.data.endTime
          this.form.content = res.data.content
          // if(this.form.callPost){
          //   this.callPostList = this.form.callPost.split(",")
          // }
        }
      })
      //获取巡检任务的名称及要求
      listTaskType({taskType: 'INSPECTION'}).then(res => {
        if (res.data) {
          this.typeName = res.data[0].taskName
          this.form.remark = res.data[0].taskDemand
        }
      })
      this.open = true
    },
    /** 生成巡检任务按钮 */
    submitForm: function () {
      // this.form.callPost = this.callPostList.join(",")
      this.$refs["form"].validate(valid => {
        if (valid) {
          buildPatrolTask(this.form).then(res => {
            if (res.data) {
              this.$message.success("生成成功")
            } else {
              this.$message.error('生成失败')
            }
            this.open = false;
            this.handleSearch();
          });
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
